<?php
namespace Mob\Controller;

class IndexController extends MobBaseController
{

    /**
     *  交易大厅
     */
    public function index()
    {
        /**
         * 域名类型列表
         * @var unknown
         */
        $categoryId=I('category',0);
        $categoryArr = D('DomainType')->order('id')->select();
        if($categoryId==0){
            $categoryId=$categoryArr[0]['id'];
        }
        $categoryobj=D('DomainType')->find($categoryId);
        $this->assign('categoryobj', $categoryobj);
        $this->assign('categoryArr', $categoryArr);
        
        
        $sellDomCond['uid']=session('uid');
        $sellDomCond['domain_typeid']=$categoryId;
        $sellDomCond['is_mortage']=1;
        $sellDomCond['trans_status']=1;
        $sellDomCond['check_status']=3;
        $sellCount=M('UserDomains')->where($sellDomCond)->count();
        $this->assign('sellCount', $sellCount);
    
        /**
         * 一周之内交易曲线图数据
         * @var unknown
        */
        $datecondi['createdate'] = array(array('egt',date("Y-m-d",strtotime("-6 day")).' 00:00:00'),array('elt',date("Y-m-d",strtotime("2 day")).' 00:00:00'));
        $datecondi['domain_type_id']=$categoryId;
        $dateLogArr = M('DomainTranslog')->where($datecondi)->order('createdate ')->select();
        foreach ($dateLogArr as $log) {
            $dateLogArrstr = $dateLogArrstr . "," . $log['createdate'];
            $priceLogArrstr = $priceLogArrstr . "," . $log['price'];
        }
        $dateLogArrstr = substr($dateLogArrstr,1);
        $priceLogArrstr = substr($priceLogArrstr,1);
        $this->assign('dateLogArr', $dateLogArrstr);
        $this->assign('priceLogArr', $priceLogArrstr);
        //  今日成交总额 select sum(totel_price) from  dd_domain_translog where createdate=now()
        $dayAlltotelPrice = M('DomainTranslog')->where('createdate>='.strtotime(date('Y-m-d')))->sum('totel_price');
        $this->assign('dayAlltotelPrice', intval($dayAlltotelPrice));
    
        // 今日成交量 select sum(trans_number) from  dd_domain_translog where createdate=now()
        $dayAllTransnumber = M('DomainTranslog')->where('createdate>='.strtotime(date('Y-m-d')))->sum('trans_number');
        $this->assign('dayAllTransnumber', $dayAllTransnumber);
         
        // 此类型域名最近两笔成交价：select totel_price from  dd_domain_translog where domain_type_id=#{} order by createdate desc limit 2;
        $conditsearch['domain_type_id']=$categoryId;
        $twoTopriceArr = M('DomainTranslog')->field('totel_price')->where($conditsearch)->order('createdate desc ')->limit('2')->select();
        if(empty($twoTopriceArr)) {
            $this->assign('nowTopriceArr', 0.00);
            $this->assign('nowRange', 2);
        } else if(count($twoTopriceArr)<2) {
            $this->assign('nowTopriceArr', intval($twoTopriceArr['0']['totel_price']));
            $this->assign('nowRange', 2);
        } else {
            $this->assign('nowTopriceArr', intval($twoTopriceArr['0']['totel_price']));
            if($twoTopriceArr['0']['totel_price']>=$twoTopriceArr['1']['totel_price']) {
                $this->assign('nowRange', 2);
            } else {
                $this->assign('nowRange', 1);
            }
        }
    
        //此类型域名单个最高单价： select price from  dd_domain_translog where domain_type_id=#{} order by price desc limit 1;
        $priceHigh = M('DomainTranslog')->field('price')->where($conditsearch)->order('price desc ')->limit('1')->select();
        $this->assign('priceHigh', intval($priceHigh[0]['price']));
         
        //此类型域名单个最低单价： select price from  dd_domain_translog where domain_type_id=#{} order by price limit 1;
        $priceLow = M('DomainTranslog')->field('price')->where($conditsearch)->order('price ')->limit('1')->select();
        $this->assign('priceLow', intval($priceLow[0]['price']));
    
        //此类型域名累积成交量 select sum(trans_number) from  dd_domain_translog where domain_type_id=#{};
        $domainLjNumber = M('DomainTranslog')->where($conditsearch)->sum('trans_number');
        $this->assign('domainLjNumber', $domainLjNumber);
         
        // 此类型域名累积成交总额  select sum(totel_price) from  dd_domain_translog where domain_type_id=#{};
        $domainLjPrice = M('DomainTranslog')->where($conditsearch)->sum('totel_price');
        $this->assign('domainLjPrice', intval($domainLjPrice));
    
    
        $dabgwsearch['status']=0;
        //档位 卖 select price,trans_number,totel_price from dd_domain_translog where domain_type_id=#{} and trans_type=2 order by price limit 1,5;//卖
        $dabgwsearch['domain_typeid']=$categoryId;
        $dabgwsearch['trans_type']=2;
    
        $dangweiSellArr = M('UserEntrust')->field('SUM(surplus) AS surplus,unit_price')->where($dabgwsearch)->
        group('unit_price')->order('unit_price desc')->limit('0,5')->select();
        $this->assign('dangweiSellArr', $dangweiSellArr);
    
        $darray=array();
        for ($x=count($dangweiSellArr); $x>0; $x--) {
            array_push($darray,$x);
        }
        $this->assign('darray', $darray);
        //档位 买  select price,trans_number,totel_price from dd_domain_translog where domain_type_id=#{} and trans_type=1 order by price limit 1,5;//买
        $dabgwsearch['trans_type']=1;
        $dangweiMallArr = M('UserEntrust')->field('SUM(surplus) AS surplus,unit_price')->where($dabgwsearch)->group('unit_price')->order('unit_price desc')->limit('0,5')->select();
        $this->assign('dangweiMallArr', $dangweiMallArr);
        
        //我的委托
        $list = D('UserEntrust')->getList(array('uid' => session('uid'),'domain_typeid' => $categoryId,'status'=>0));
        $count = D('UserEntrust')->getCount(array('uid' => session('uid'),'domain_typeid' => $categoryId,'status'=>0));
        $this->assign('list', $list);
        $this->assign('count', $count);
    
        $this->assign('categoryId', $categoryId);
    
        /**
         * 委托买卖
        */
        //可用金额
        $uidcond['uid']=session('uid');
        $availMoney = M('UserAssets')->field('avail_money')->where($uidcond)->select();
        $this->assign('avail_money', $availMoney[0]['avail_money']);
         
        //此类型域名今日授信额度   select  credit_limit from dd_domain_type where id=#{}
        $idcond['id']=$categoryId;
        $credit_limit = M('DomainType')->field('credit_limit')->where($idcond)->select();
        $this->assign('credit_limit', $credit_limit[0]['credit_limit']);
    
        //卖域名手续费率
        $feetype['type']=2;
        $fee = M('TradeFee')->field('rate')->where($feetype)->select();
        $this->assign('rate', $fee[0]['rate']);
    
        $this->display('tradinghall/index');
    }
    
    public function monthtralog(){
        $cateid=I('category',0);
        $categoryArr = D('DomainType')->field('id')->order('id')->select();
        if($cateid==0){
            $cateid=$categoryArr[0]['id'];
        }
        //一月内的交易
        $nowTime=date('y-m-d H:m:s',time());
        $beginTime=date("Y-m-d",strtotime("-1 month"));
        $msql=M();
        $monthList=$msql->query("SELECT SUM(totel_price) AS trans_amount,COUNT(id) AS trans_number, DATE_FORMAT(createdate,'%Y-%m-%d') AS create_time  FROM dd_domain_translog WHERE domain_type_id=".$cateid." AND createdate>'".$beginTime." 00:00:00' AND createdate<='".$nowTime."' GROUP BY create_time ORDER BY create_time");
        $this->ajaxReturn(array('monthlist'=>$monthList));exit();
    }
    
    
    public function buycredits(){
        $domtype=I('category','');
        $term=I('term','');
        $dao=M('InvesDomcate');    
        $list=$dao->join('LEFT JOIN  dd_investor_info ON dd_inves_domcate.inve_id=dd_investor_info.id')
        ->join("LEFT JOIN dd_user_assets ON dd_investor_info.uid=dd_user_assets.uid")->
        field("dd_inves_domcate.id,dd_inves_domcate.rate,dd_inves_domcate.credit,dd_user_assets.creditmoney")->
        where('dd_inves_domcate.domtypeid='.$domtype.' and dd_investor_info.uid!='.session('uid').' AND dd_user_assets.creditmoney>0'.' and dd_investor_info.term_begin<='.$term.' and dd_investor_info.term_end>='.$term)->select();
        $this->ajaxReturn(array('list'=>$list));exit();
    }
    
    public function sellcount(){
       $cateid=I('cateid','');
       if($cateid==''){
           $this->ajaxReturn(array('data'=>''));exit();
       }
       $credflowList = M('CredFlow')->field('cred_code')->
       where('domtypeid='.$cateid.' and loanduid='.session('uid').' and status in (1,3)')->group('cred_code')->select();
       $loadArr=array();
       $counArr=array();
      foreach ($credflowList as $cred){
          $count=M('CredflowDomian')->where("cred_code='".$cred['cred_code']."'")->count();
          $credit=M('CredflowDomian')->field('credit')->where("cred_code='".$cred['cred_code']."'")->select();
          $map=array('count'=>$count,'flowid'=>$cred['cred_code'],'credit'=>$credit[0]['credit']);
          array_push($counArr, $map);
      }
       $this->ajaxReturn(array('data'=>$counArr));exit();
    }
    
    public function credtdetail(){
        $flowid=I('flowid','');//cred_code
        if($flowid==''){
            $this->ajaxReturn(array('data'=>''));exit();
        }
        $count = M('CredFlow')->where("cred_code='".$flowid."'")->count();
        if(empty($count) || $count==0){
            $this->ajaxReturn(array('data'=>''));exit();
        }
       $money= M('CredFlow')->where("cred_code='".$flowid."'")->sum('money');
       $persionnum=M('CredFlow')->where("cred_code='".$flowid."'")->sum('persionnum');
       $rpersionum=M('CredFlow')->where("cred_code='".$flowid."'")->sum('rpersionum');
       
       $creflowarr= M('CredFlow')->field('money,persionnum,rpersionum,interest,penalty,mangrate')->where("cred_code='".$flowid."' and status in (1,3)")->select();
       foreach ($creflowarr as $flow){
           $huankuan+=$flow['money']+($flow['interest']*($flow['persionnum']-$flow['rpersionum']))+$flow['penalty']+$flow['mangrate'];
       }
       $this->ajaxReturn(array('data'=>' 借款'.$money.'￥,共'.$persionnum.'期,已还'.$rpersionum.'期','sellmoney'=>$huankuan));exit();
    }
    
    public function flushtra(){
        
        $datecondi['createdate'] = array(array('egt',date("Y-m-d").' 00:00:00'),
            array('elt',date("Y-m-d").' 23:59:59'));
        
        $daytraprice=M('DomainTranslog')->where($datecondi)->sum('totel_price');
        $alltraprice=M('DomainTranslog')->sum('totel_price');
        
        $daytranumber=M('DomainTranslog')->where($datecondi)->sum('trans_number');
        $alltranumber=M('DomainTranslog')->sum('trans_number');
        
        if(empty($daytraprice)){
            $daytraprice=0;
        }
        if(empty($alltraprice)){
            $alltraprice=0;
        }
        if(empty($daytranumber)){
            $daytranumber=0;
        }
        if(empty($alltranumber)){
            $alltranumber=0;
        }
        
        $this->ajaxReturn(array(
            'daytranum'=>$daytranumber,
            'daytraprice' => $daytraprice,
            'alltranum' => $alltranumber,
            'alltraprice' => $alltraprice
        ));//0错误，1成功
        exit;
    }
}